package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.DefecttypeForm;
import com.woniuxy.controller.form.SeverityForm;
import com.woniuxy.controller.info.SeverityInfo;
import com.woniuxy.controller.info.TypeInfo;
import com.woniuxy.controller.info.UserInfo;
import com.woniuxy.model.Defectseverity;
import com.woniuxy.model.Defecttype;
import com.woniuxy.service.DefectseverityService;
import com.woniuxy.service.DefecttypeService;
import com.woniuxy.utils.MyException;
import com.woniuxy.utils.Result;
import com.woniuxy.utils.TimeMethodsutil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@RestController
@Api(tags = "缺陷类型接口")
@RequestMapping("/defecttype")
public class DefecttypeController {
    @Resource
    private DefecttypeService defecttypeService;
    /**
     * 描述：查询所有
     * @return all
     */
    @PostMapping("/findAll")
    @ApiOperation("查询缺陷类型")
    public Result findAll(){
        List<TypeInfo> all = defecttypeService.findAll();

        return Result.success(200,"success",all);
    }

    /**
     * 描述：新增缺陷类型
     * @return num
     */
    @ApiOperation("新增缺陷类型")
    @PostMapping("/addType")
    public Result addPriority(@RequestBody DefecttypeForm defecttypeForm) throws MyException {

        //获取用户信息
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        String nowTimeToString = TimeMethodsutil.getNowTimeToString();
        defecttypeForm.setDefecttypeCreatename(userInfo.getUsername());
        defecttypeForm.setDefecttypeCreatenameId(userInfo.getId());
        defecttypeForm.setDefecttypeCreatetime(nowTimeToString);
        Integer add= defecttypeService.add(defecttypeForm);
        if(add>0){
            return Result.success(200,"success",add);
        }else{
            throw new MyException(405,"新增失败");
        }

    }


    /**
     * 描述：批量删除
     * @return delectid
     */
    @ApiOperation("批量删除")
    @PostMapping("/delectMany")
    public Result delectMany(@RequestBody List<Integer> ids) {
        System.out.println("================" + ids);

        Integer delectid = defecttypeService.delectByIds(ids);
        return Result.success(200, "success", delectid);
    }

    /**
     * 描述：单个删除
     * @return delectid
     */
    @ApiOperation("单个删除")
    @PostMapping("/delectbyid/{defecttypeId}")
    public  Result delectByid(@PathVariable Integer defecttypeId) throws MyException {


        boolean b = defecttypeService.removeById(defecttypeId);
        if (b){
            return Result.success(200,"success",b);
        }else {
            throw new MyException(405,"删除失败");
        }

    }

    /**
     * 修改严重程度
     * @param DefecttypeForm
     * @return
     * @throws MyException
     */
    @ApiOperation("修改严重程度")
    @PostMapping("/update")
    public Result updatePriority(@RequestBody DefecttypeForm DefecttypeForm) throws MyException {

        boolean change = defecttypeService.change(DefecttypeForm);

        if (change){
            return Result.success(200,"success",change);
        }else {
            throw new MyException(405,"服务器繁忙");
        }
    }


    /**
     * 修改状态
     * @param DefecttypeForm
     * @return
     */
    @ApiOperation("修改状态")
    @PostMapping("/changeState")
    public Result changeState(@RequestBody DefecttypeForm DefecttypeForm){
        Integer integer = defecttypeService.changeState(DefecttypeForm);
        return Result.success(200,"success",integer);
    }

}

